#coding=utf-8
from uliweb.form import *
from uliweb.i18n import ugettext_lazy as _

class ChangeInfoForm(Form):
    form_buttons = Submit(value=_('Save'), _class="submit")
    form_title = _('Change Basic Information')

    email = StringField(label=_('Email'))
    image = ImageField(label=_('Portrait'))
    action = HiddenField(default='changeinfo')
    
class LoginForm(Form):
    form_buttons = Submit(value=_('Login'), _class="button")
    form_title = _('Login')
    
    username = UnicodeField(label=_('Username'), required=True)
    password = PasswordField(label=_('Password'), required=True)
    rememberme = BooleanField(label=_('Remember Me'))
    next = HiddenField()
    
    def validate_username(self, data):
        from uliweb.orm import get_model
        
        User = get_model('user')
        user = User.get(User.c.username==data)
        if not user:
            return 'User "%s" does not exist!' % data
        self._user = user
    
    def form_validate(self, all_data):
        from uliweb import settings
        from uliweb.orm import get_model
        
        User = get_model('user')
        user = User.get(User.c.username==all_data['username'])
        if not self._user.check_password(all_data['password']):
            return {'password' : _('Password is not right.')}
        if all_data['password'] == settings.PARA.DEFAULT_PASSWORD:
            return {'password' : '请不要使用缺省口令，马上 <a href="/config/user/change_password?user_id=%s">点击这里</a> 修改' % user.id}
        